package com.woniu.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.car.domain.TBackstageAccount;
import com.woniu.car.domain.TRole;
import com.woniu.car.exception.AccountException;
import com.woniu.car.exception.ParamException;
import com.woniu.car.mapper.TBackstageAccountMapper;
import com.woniu.car.mapper.TRoleMapper;
import com.woniu.car.param.RoleParam;
import com.woniu.car.service.TRoleService;
import com.woniu.car.util.AccountStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WWX
 * @since 2021-06-19
 */
@Service
public class TRoleServiceImpl extends ServiceImpl<TRoleMapper, TRole> implements TRoleService {
    @Resource
    private TBackstageAccountMapper tBackstageAccountMapper;
    /**
     *@Author 王维鑫
     *@Description 新增角色
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public void insertRole(RoleParam param) throws ParamException {
        //查询员工ID是否存在
        Integer registerId = param.getRegisterId();
        TBackstageAccount tBackstageAccount = tBackstageAccountMapper.selectById(registerId);
        if(null==tBackstageAccount){
            throw new AccountException("该员工不存在,请检查");
        }
        //查询该角色是否存在
        TRole tRole1 = new TRole();
        tRole1.setRoleName(param.getName());
        List<TRole> tRoles = this.queryRole(tRole1);
        if(tRoles.size()!=0){
            throw new ParamException("该角色以存在请勿重复添加");
        }
        tRole1.setRoleStatus(AccountStatus.NORMAL);
        tRole1.setRegisterId(param.getRegisterId());
        boolean save = this.save(tRole1);
        if(save==false){
            throw new AccountException("新增角色失败");
        }
    }
    /**
     *@Author 王维鑫
     *@Description 修改角色
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public void updateRole(TRole tRole)  {
        boolean b = this.updateById(tRole);
        if(b==false){
            throw new AccountException("修改角色失败");
        }
    }
    /**
     *@Author 王维鑫
     *@Description 查看角色列表
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public List<TRole> showRole() {
        List<TRole> list = this.list();
        return list;
    }
    /**
     *@Author 王维鑫
     *@Description 查询角色
     *@Param
     *@return
     **/
    @Override
    @Transactional
    public List<TRole> queryRole(TRole tRole) {
        QueryWrapper<TRole> wrapper = new QueryWrapper<>();
        if(null!=tRole.getRoleId()){
            wrapper.eq("role_id",tRole.getRoleId());
        }
        if(null!=tRole.getRoleName() ){
            wrapper.eq("role_name",tRole.getRoleName());
        }
        if(null!=tRole.getRoleStatus()){
            wrapper.eq("role_status",tRole.getRoleStatus());
        }
        if(null!=tRole.getRegisterId()){
            wrapper.eq("register_id",tRole.getRegisterId());
        }
        List<TRole> list = this.list(wrapper);
        return list;
    }
}
